﻿// 当使用TOF V2时，请取消下行注释，将代码编译为适用于TOF V2版本
// 该代码主要用于处理 RightClient 和 RightV3Client
//
//#define TOFV2

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

using Tencent.OA.Common.Web.Security;
using Tencent.OA.Framework.RightWay.Entity;
using Tencent.OA.Framework.RightWay.Proxy;
using Tencent.OA.Common.Organization;
using System.Web.SessionState;

#if TOFV2
using TOF_RightClient = Tencent.OA.Framework.RightWay.Proxy.RightClient;
#else
using TOF_RightClient = Tencent.OA.Framework.RightWay.Proxy.RightV3Client;
#endif

namespace Tencent.OA.ACM.App
{
    public class SecurityModule : IHttpModule, IReadOnlySessionState
    {
        /// <summary>
        /// SESSION登录标识
        /// </summary>
        public static readonly string SessionKey = "SESSION_STAFF";

        #region IHttpModule 成员

        public void Dispose()
        {
            //throw new NotImplementedException();
        }

        public void Init(HttpApplication context)
        {
            context.AcquireRequestState += new EventHandler(context_AcquireRequestState);
        }

        protected void context_AcquireRequestState(object sender, EventArgs e)
        {
            HttpContext context = ((HttpApplication)sender).Context;
            if (context.Request.IsAuthenticated)
            {
                if (context.Session != null && (context.Session["Session_UserName"] == null || context.Session[Authentication.SessionKey] == null))
                {
                    string userName = context.User.Identity.Name;
                    BancheStaff staff = new BancheStaff(userName);
                    staff.OperationList = Authentication.GetStaffOperation(staff.Id);

                    context.Session[Authentication.SessionKey] = staff;
                    context.Session["Session_UserName"] = new Tencent.OA.Framework.Organization.Staff(staff.Id);
                }               
            }

            #region
            //HttpContext context = ((HttpApplication)sender).Context;
            //if (context.Session != null && context.Session[SessionKey] == null)
            //{
            //    PassportAuthentication passport;//  = new PassportAuthentication();
            //    string ticketStr = context.Request.QueryString["ticket"];
            //    if (!string.IsNullOrEmpty(ticketStr))
            //        passport = new PassportAuthentication(ticketStr);
            //    else
            //        passport = new PassportAuthentication();

            //    // 如果未通过身份验证，则跳转到TOF登陆界面
            //    if (!passport.IsAuthenticated())
            //    {
            //        passport.SignIn(
            //            HttpUtility.UrlDecode(MvcApplication.SystemName),
            //            //HttpUtility.UrlEncode(MvcApplication.SystemFullPath)
            //            HttpUtility.UrlEncode(context.Request.Url.AbsoluteUri)
            //        );
            //    }
            //    else
            //    {
            //        BancheStaff staff = new BancheStaff(passport.Ticket.LoginName);
            //        // 获取用户所有角色
            //        //staff.Roles = TOF_RightClient.GetAllRole(MvcApplication.SystemID, passport.Ticket.StaffId);
            //        staff.OperationList = GetStaffOperation(passport.Ticket.StaffId); //TOF_RightClient.GetOperations(MvcApplication.SystemID, passport.Ticket.StaffId);
            //        //staff.CompanyScope = codes;
            //        context.Session[SessionKey] = staff;
            //        context.Session["Session_UserName"] = new Tencent.OA.Framework.Organization.Staff(passport.Ticket.StaffId);

            //        //将用户信息附加到当前Http上下文
            //        //context.User = new TencentPassportPrincipal(staff);
            //        context.Session.Timeout = 20;
            //    }
            //}
            #endregion
        }

        #endregion
    }
}
